package jdbchomework;

import java.sql.*;
import java.util.Scanner;

public class Test {
	static String sql = "", com = "";

	public static void main(String args[]) {
		String driverName = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@172.17.1.6:tarena";
		String userName = "";
		String pwd = "tarena";
		Connection con = null;
		Statement st = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url, userName, pwd);
			System.out.println("SQL>");
			Scanner sca = new Scanner(System.in);
			while (true) {
				com = sca.next();
				sql = sql + " " + com;
				if (com.endsWith(";")) {
					break;
				}
				sql = sql.substring(0, sql.length() - 1);
			}
			st.executeUpdate(sql);
			rs = ps.executeQuery(sql);
			try {
				ResultSetMetaData meta = rs.getMetaData();
				int n = meta.getColumnCount();
				StringBuffer sb = new StringBuffer();
				while (rs.next()) {
					for (int i = 1; i < n; i++) {
						sb.append(meta.getColumnName(i) + ":");
						sb.append(rs.getString(i) + " ;");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
